package com.example.cloud.controller.system;

import com.example.cloud.constants.CommonConstants;
import com.example.cloud.controller.common.BaseController;
import com.example.cloud.model.dto.QueryModel;
import com.example.cloud.model.dto.Result;
import com.example.cloud.model.system.Account;
import com.example.cloud.model.system.Organ;
import com.example.cloud.service.system.AccountService;
import com.example.cloud.service.system.OrganService;
import com.example.cloud.service.system.UserService;
import com.example.cloud.utils.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by wangtao on 2018/1/26.
 */
@RestController
@RequestMapping("/account")
public class AccountController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(AccountController.class);

    @Autowired
    private AccountService accountService;

    @Autowired
    private UserService userService;

    @Autowired
    private OrganService organService;

    @PostMapping("/search")
    public Result search(@RequestBody QueryModel model) {
        return this.singleQuery(model, Account.class);
    }

    @PostMapping("/saveOrUpdate")
    public void saveOrUpdate(@RequestBody Account account) {
        // 追加组织机构父节点名称
        StringBuilder sb = new StringBuilder();
        organService.appendName(account.getOrganId(),sb);
        // 保存账户
        account.setOrganName(sb.toString());
        accountService.doSave(account);
    }

    @GetMapping("/getOrgans")
    public Result getOrgans() {
        List<List<Organ>> list = new ArrayList<List<Organ>>();
        List<Organ> organs = organService.findAllFromCache();
        if (!organs.isEmpty()) {
            Map<Integer, List<Organ>> mapList = organs.stream().collect(Collectors.groupingBy(Organ::getLevel));
            for (Map.Entry<Integer, List<Organ>> entry : mapList.entrySet()) {
                int level = entry.getKey().intValue();
                if (level == 0) {
                    list.add(entry.getValue());
                } else {
                    list.add(new ArrayList<Organ>());
                }
            }
        }
        return ResultUtil.success(list);
    }

    @GetMapping("/getChildren/{parentId}")
    public Result getChildren(@PathVariable Long parentId) {
        Result result = new Result();
        result.setCode(CommonConstants.SUCCESS);
        result.setData(organService.findOneFromCache(parentId));
        Map<String, Object> params = new HashMap<String, Object>(1);
        List<Organ> children = organService.findChildren(parentId);
        if(children == null){
            children = new ArrayList<Organ>();
        }
        params.put("children", children);
        result.setParams(params);
        return result;
    }

    @PostMapping("/freeze")
    public void freeze(@RequestBody List<Long> ids){
        accountService.doFreeze(ids);
    }

    @PostMapping("/use")
    public void use(@RequestBody List<Long> ids){
        accountService.doUse(ids);
    }

    @PostMapping("/delete")
    public void delete(@RequestBody List<Long> ids){
        accountService.doDelete(ids);
    }

}
